<?php
/**
 * ClassName: prescript
 * Description: 处方控制器
 * @Author 周昌
 * @Create 2023/11/30 11:05
 */

namespace app;


use epii\server\Args;
use libs\Tool;
use model\ArchivesModel;
use model\CardLogModel;
use model\PrescriptModel;
use model\WaitModel;
use service\admin\CardService;
use service\admin\HospitalService;
use service\admin\MemberService;
use service\admin\PrescriptService;
use service\admin\WaitService;

class prescript extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }

    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_status=PrescriptService::prescriptStatusSelect();
        $this->_as_payment_status=PrescriptService::prescriptPaymentSelect();
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_id=$this->adminInfo['hospital_id'];
        if($hospital_id){
            $map[]=['hospital_id','=',$hospital_id];
        }
        $realname = Args::params('realname');
        if ($realname) {
            $map[] = ['realname', "LIKE", '%' . $realname . '%'];
        }
        $phone = Args::params('phone');
        if ($phone) {
            $map[] = ['phone', "=", $phone];
        }
        //处方编号
        $id = Args::params('id/d');
        if ($id) {
            $map[] = ['id', "=", $id];
        }
        //取药状态
        $status = Args::params('status/d');
        if (is_int($status)) {
            $map[] = ['status', "=", $status];
        }
        //支付状态
        $payment_status = Args::params('payment_status/d');
        if (is_int($payment_status)) {
            $map[] = ['payment_status', "=", $payment_status];
        }

        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        echo PrescriptService::getAjaxData($map);
    }
    //问诊 开药
    public function add_edit()
    {
        if (Args::isPost()){
            $wait_id = Args::params('wait_id/d');
            $case_id = Args::params('case_id/d');
            $data=Args::params('r');
            $id=PrescriptModel::getValue(['case_id'=>$case_id],'id');
            //问诊排号 结束或取消 禁止修改数据
            $wait_status=WaitModel::getValue(['id'=>$id],'status');
            if($wait_status>WaitService::$wait_status_index['progress']){
                $this->error('该问诊已结束或已取消无法保存');
            }
            //字段验证
            $this->validate($data,[
                'prescript_num|开药副数'=>'require|integer|>:0',
                'doctor_advice|医嘱信息'=>'max:300',
            ],[],false,true);
            if($id){
                //编辑
                $res=PrescriptService::editInfo($data,$id);
            }else{
                //添加
                $res=PrescriptService::addInfo($data,$wait_id);
            }
            $res['code']?$this->jsonSuccess($res['msg']):$this->jsonError($res['msg']);
        }
    }
    //取药
    public function take()
    {
        $id = Args::params('id');
        $info=PrescriptModel::getInfo(['id'=>$id]);
        if (Args::isPost()){

            $data=Args::params('r');
            //验证
            $message=[
                'status|取药状态'=>'require|integer|>:0',
            ];
            //若取药，必须支付
            if($data['status']==PrescriptService::$prescript_status_index['has_been_take']){
                $message['pay_type|支付方式']='require|integer|>=:0';
                if($data['pay_type']=='0'){
                    $message['card_id|卡id']='require|integer|>:0';
                }
                if($data['pay_type']=='1'){
                    $data['desc']=trim($data['desc']);
                    $message['desc|支付简介']='require|max:225';
                }
            }
            $this->validate($data,$message);
            if($data['status']==PrescriptService::$prescript_status_index['cancel']){
                //处方取消
                if($info['status']==PrescriptService::$prescript_status_index['cancel']){
                    $this->error('已取消禁止修改');
                }
                $res=PrescriptService::cancel($id);
                $res['code']?$this->success($res['msg']):$this->error($res['msg']);
            }elseif ($data['status']==PrescriptService::$prescript_status_index['has_been_take']){
                //取药
                if($info['status']==PrescriptService::$prescript_status_index['has_been_take']){
                    $this->error('已取药禁止修改');
                }
                $res=PrescriptService::takeAndPay($id,$data);
                $res['code']?$this->success($res['msg']):$this->error($res['msg']);
            }else{
                $this->error('药品已经被取或者处方已经被取消');
            }
        }
        $this->_as_info =$info;
        $this->_as_id = $id;
        $this->_as_status = PrescriptService::prescriptStatusSelect(1);
        $this->_as_payment_status = PrescriptService::prescriptPaymentSelect();
        $this->_as_pay_type = PrescriptService::prescriptPayTypeSelect();
        $member_id=ArchivesModel::getValue(['id'=>$info['archives_id']],'member_id');
        $this->_as_card_list=CardService::getCardList($member_id,1);

        $this->adminUiDisplay();
    }
    //处方笺详情
    public function info()
    {
        $id = Args::params('id');
//        var_dump(PrescriptService::prescriptInfo($id));
        $this->_as_info = PrescriptService::prescriptInfo($id);
        $this->adminUiDisplay();
    }

    /**
     * @return array|false|string
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     * 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断该事项下是否已经有床位，无则可以删除
        $res = PrescriptService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }

}